<!--{template /module/admin/view/head}-->
<div id="left_layout">
	<div id="main_content" class="container-fluid">
		<!--{template /module/admin/view/head_nav}-->
		<div class="row-fluid">
			<form method="post" action="{$ADMIN_SCRIPT}/poll/{$dispatches[method]}&id={$_var[gp_id]}&page={$_var[page]}&psize={$_var[psize]}" onsubmit="javascript:return check_form(this);">
				<input type="hidden" name="formsubmit" value="yes" />
				<input type="hidden" name="hdnFile01" value=""/>
				<input type="hidden" name="hdnFile02" value=""/>
				
				<div class="well widget">
					<div class="widget-header">
						<p class="text">* {lang admin.validate.require}</p>
					</div>
					<div class="widget-content">
						<fieldset>
							<label>{lang poll.index_edit.view.identity.label} *</label>
							<input type="text" class="input" name="txtIdentity" maxlength="20" placeholder="{lang poll.index_edit.view.identity.placeholder}..." value="{$poll[IDENTITY]}" /> 
							<span class="help-inline">{lang poll.index_edit.view.identity.tips}</span>
						
							<label>{lang poll.index_edit.view.title.label} *</label>
							<input type="text" class="input-xlarge"  name="txtTitle" placeholder="{lang poll.index_edit.view.title.placeholder}..." maxlength="50" value="{$poll[TITLE]}"  /> 
							<span class="help-inline">{lang poll.index_edit.view.title.tips}</span>
							
							<label>{lang poll.index_edit.view.begindate.label}</label>
							<input type="text"  class="input-medium input-time" name="txtBeginDate" maxlength="12"  value="{$poll[BEGINDATE]}"/> 
							
							<label>{lang poll.index_edit.view.enddate.label}</label>
							<input type="text"  class="input-medium input-time" name="txtEndDate" maxlength="12" value="{$poll[ENDDATE]}"/> 
							
							<label>{lang poll.index_edit.view.commend.label}</label>
							<select name="eleIsCommend">
						        <option value="0">{lang poll.index_edit.view.commend.select.0}</option>
						        <option value="3" {if $poll['ISCOMMEND'] == 3}selected="selected"{/if}>{lang poll.index_edit.view.commend.select.3}</option>
						        <option value="2" {if $poll['ISCOMMEND'] == 2}selected="selected"{/if}>{lang poll.index_edit.view.commend.select.2}</option>
						        <option value="1" {if $poll['ISCOMMEND'] == 1}selected="selected"{/if}>{lang poll.index_edit.view.commend.select.1}</option>
					        </select>
					        <span class="help-inline">{lang poll.index_edit.view.commend.tips}</span>
					        
							<label>{lang poll.index_edit.view.guest.label}</label>
							<label class="checkbox inline">
								<input type="radio" name="rdoGuest" class="fancy" value="1"  {if $poll['GUEST'] == 1}checked="checked"{/if} /> {lang poll.index_edit.view.guest.enabled}
							</label>
							<label class="checkbox inline">
                				<input type="radio" name="rdoGuest" class="fancy" value="0" {if $poll['GUEST'] == 0}checked="checked"{/if} /> {lang poll.index_edit.view.guest.disabled}
							</label>
							
            				<hr/>
            				<label>{lang poll.index_edit.view.type.label}</label>
            				<label class="checkbox inline">
								<input type="radio" name="rdoOptionType" class="fancy" value="1"  {if $poll['OPTIONTYPE'] == 1}checked="checked"{/if} /> {lang poll.index_edit.view.type.option.1} 
							</label>
							<label class="checkbox inline">
                				<input type="radio" name="rdoOptionType" class="fancy" value="0" {if $poll['OPTIONTYPE'] == 0}checked="checked"{/if} /> {lang poll.index_edit.view.type.option.0} 
							</label>
							
							<hr/>
            				<label>{lang poll.index_edit.view.limit.label}</label>
            				<input type="text" class="input-mini"  name="txtLimitNum" maxlength="6" value="{$poll[LIMITNUM]}"/> {lang poll.index_edit.view.limit.vote}
            				
            				<label>{lang poll.index_edit.view.content.label}</label>
							<textarea class="span10" rows="6" cols="45" name="txtContent">{$poll[CONTENT]}</textarea>
							<br/>
							<span class="help-inline">{lang poll.index_edit.view.content.tips}</span>
							
							<hr/>
							<h5>{lang poll.index_edit.view.option.label} *</h5>
							<div id="div_poll_option">
	            				<table class="table table-bordered swfupload" style="width:auto;">
									<thead>
										<tr>
											<th width="40">{lang poll.index_edit.view.option.th.operate}</th>
											<th width="60">{lang poll.index_edit.view.option.th.displayorder}</th>
											<th width="120">{lang poll.index_edit.view.option.th.name}</th>
											<th>{lang poll.index_edit.view.option.th.remark}</th>
								            <th width="40">{lang poll.index_edit.view.option.th.pic}</th>
								            <th width="130"></th>
										</tr>
									</thead>
									<tbody id="tbl_poll_option_body">
										<!--{loop $poll_options $loop $poll_option}-->
										<tr id="up_poll_option_{echo $loop + 1;}">
								        	<td>
								        		<a class="btn btn-mini btn-primary" href="javascript:void(null);">{lang admin.operation.delete}</a>
								        	</td>
								            <td>
								                <input type="text" class="input-mini" name="optiondisplayorder[{$poll_option[POLL_OPTIONID]}]" value="{$poll_option[DISPLAYORDER]}" maxlength="4"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="optiontitle[{$poll_option[POLL_OPTIONID]}]" value="{$poll_option[TITLE]}" maxlength="50"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="optionsummary[{$poll_option[POLL_OPTIONID]}]" value="{$poll_option[SUMMARY]}" maxlength="50"/>
								            </td>
								            <td class="swfupload">
												<!--{if is_array($poll_option['FILE01'])}-->
												<img class="img" src="{$poll_option[FILE01][0]}" data="{$poll_option[FILE01][4]}" width="35" style="width:35px; height:35px;" border="0"/>
												<!--{else}-->
												<img class="img" src="static/images/no-picture.jpg" data="" width="35" style="width:35px; height:35px;" />
												<!--{/if}-->
											</td>
											<td>
												<div class="upload-button" type="" ref="poll_option_{echo $loop + 1;}" data="{$poll_option[POLL_OPTIONID]}"><span></span></div>
								            </td>
								        </tr>
										<!--{/loop}-->
									    <tr id="up_poll_option_0" class="tmp">
								        	<td>{lang poll.index_edit.view.option.td.button.label}</td>
								            <td>
								                <input type="text" class="input-mini" name="newoptiondisplayorder[]" value="" maxlength="4"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="newoptiontitle[]" value="" maxlength="50"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="newoptionsummary[]" value="" maxlength="50"/>
								            </td>
								            <td class="swfupload">
												<img class="img" src="static/images/no-picture.jpg" data="" width="35" style="width:35px; height:35px;" />
											</td>
											<td>
												<div class="upload-button" type="new" ref="poll_option_0" data=""><span></span></div>
								            </td>
								        </tr>
								        <tr>
								            <td colspan="6">
								            	<input type="button" id="btn_add_poll_option" name="btnAddPollOption" class="btn btn-small" value="{lang poll.index_edit.view.option.td.button.add}" /> 
								            	{lang poll.index_edit.view.option.td.button.tips}
								            </td>
								        </tr>
									</tbody>
								</table>
							</div>
							
							<label>{lang poll.index_edit.view.isaward.label}</label>
							<label class="checkbox inline">
								<input type="radio" name="rdoIsAward" class="fancy" value="1"  {if $poll['ISAWARD'] == 1}checked="checked"{/if} /> {lang poll.index_edit.view.isaward.enable}
							</label>
							<label class="checkbox inline">
                				<input type="radio" name="rdoIsAward" class="fancy" value="0" {if $poll['ISAWARD'] == 0}checked="checked"{/if} /> {lang poll.index_edit.view.isaward.disable}
							</label>
							
							<div id="div_award" style="{echo $poll['ISAWARD'] == 1 ? '' : 'display:none;';}">
								<h5>{lang poll.index_edit.view.isaward.title}</h5>
	            				
	            				<table class="table table-bordered swfupload" style="width:auto;">
									<thead>
										<tr>
											<th width="40">{lang poll.index_edit.view.isaward.th.operate}</th>
											<th width="60">{lang poll.index_edit.view.isaward.th.displayorder}</th>
											<th width="120">{lang poll.index_edit.view.isaward.th.name}</th>
								            <th width="60">{lang poll.index_edit.view.isaward.th.num}</th>
								            <th width="40">{lang poll.index_edit.view.isaward.th.pic}</th>
								            <th width="130"></th>
										</tr>
									</thead>
									<tbody id="tbl_body">
										<!--{loop $poll_awards $loop $poll_award}-->
										<tr id="up_{echo $loop + 1;}">
								        	<td>
								        		<a class="btn btn-mini btn-primary" href="javascript:void(null);">{lang admin.operation.delete}</a>
								        	</td>
								            <td>
								                <input type="text" class="input-mini" name="displayorder[{$poll_award[POLL_AWARDID]}]" value="{$poll_award[DISPLAYORDER]}" maxlength="4"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="cname[{$poll_award[POLL_AWARDID]}]" value="{$poll_award[CNAME]}" maxlength="50"/>
								            </td>
								            <td>
								            	<input type="text" class="input-mini" name="num[{$poll_award[POLL_AWARDID]}]" value="{$poll_award[NUM]}" maxlength="6"/>
								            </td>
								            <td class="swfupload">
												<!--{if is_array($poll_award['FILE01'])}-->
													<img class="img" src="{$poll_award[FILE01][0]}" data="{$poll_award[FILE01][4]}" width="35" style="width:35px; height:35px;" border="0"/>
												<!--{else}-->
													<img class="img" src="static/images/no-picture.jpg" data="" width="35" style="width:35px; height:35px;" />
												<!--{/if}-->
											</td>
											<td>
												<div class="upload-button" type="" ref="{echo $loop + 1;}" data="{$poll_award[POLL_AWARDID]}"><span></span></div>
								            </td>
								        </tr>
										<!--{/loop}-->
									    <tr id="up_0" class="tmp">
								        	<td>{lang poll.index_edit.view.isaward.td.button.label}</td>
								            <td>
								                <input type="text" class="input-mini" name="newdisplayorder[]" value="" maxlength="4"/>
								            </td>
								            <td>
								            	<input type="text" class="input-xlarge" name="newcname[]" value="" maxlength="50"/>
								            </td>
								            <td>
								            	<input type="text" class="input-mini" name="newnum[]" value="" maxlength="6"/>
								            </td>
								            <td class="swfupload">
												<img class="img" src="static/images/no-picture.jpg" data="" width="35" style="width:35px; height:35px;" />
											</td>
											<td>
												<div class="upload-button" type="new" ref="0" data=""><span></span></div>
								            </td>
								        </tr>
								        <tr>
								            <td colspan="6">
								            	<input type="button" id="btn_add_award" name="btnAddAward" class="btn btn-small" value="{lang poll.index_edit.view.isaward.td.button.add}" />
								            </td>
								        </tr>
									</tbody>
								</table>
							</div>
					   </fieldset>
					   
					   <hr/>
					   <label>{lang poll.index_edit.view.file.label}</label>
					   <table class="swfupload">
							<tr>
								<td width="160" valign="top" align="center">
									<p class="img" id="up_file01">
										<!--{if is_array($poll['FILE01'])}-->
										<img src="{$poll[FILE01][0]}" data="{$poll[FILE01][4]}" width="160" style="width:160px;" border="0"/>
										<!--{else}-->
										<img src="static/images/no-picture.jpg" data="" width="160" style="width:160px;" />
										<!--{/if}-->
									</p>
									<hr/>
									<div class="upload-button"><span id="swfu_file01"></span></div>
								</td>
								<td width="50"></td>
								<td width="10" style="border-left:1px solid #ccc;"></td>
								<td width="30"></td>
								<td width="240" valign="top" align="center">
									<p class="img" id="up_file02">
										<!--{if is_array($poll['FILE02'])}-->
										<img src="{$poll[FILE02][0]}" data="{$poll[FILE02][4]}" width="240" style="width:240px;" border="0"/>
										<!--{else}-->
										<img src="static/images/no-picture.jpg" data="" width="240" style="width:240px;" />
										<!--{/if}-->
									</p>
									<hr/>
									<div class="upload-button"><span id="swfu_file02"></span></div>
								</td>
							</tr>
					   </table>
					   
					   <hr/>
					   
					   <fieldset>
					   		<!--{if $dispatches['method'] == '_pub'}-->
							<button type="submit" class="btn btn-small btn-primary" {$dispatches[operations][add]}>{lang admin.operation.submit}</button>
							<!--{else}-->
							<button type="submit" class="btn btn-small btn-primary" {$dispatches[operations][edit]}>{lang admin.operation.submit}</button>
							<!--{/if}-->
							
							<button type="reset" class="btn btn-small">{lang admin.operation.reset}</button>
							<button type="button" class="btn btn-small" onclick="javascript:location.href='{$ADMIN_SCRIPT}/poll&page={$_var[page]}&psize={$_var[psize]}';">{lang admin.operation.go.back}</button>
					   </fieldset>
					   </div>
					</div>
				</div>	
			</form>
		</div>
	</div>
</div> 

<script type="text/javascript" src="static/plugins/swfupload.js"></script>
<script type="text/javascript" src="static/plugins/swfupload_handlers.js"></script>
<script type="text/javascript" src="static/plugins/swfupload_images.js"></script>

<script>
var award_count = {echo count($poll_awards) + 0;};
var poll_option_count = {echo count($poll_options) + 0;};

var swfu_file01 = null;
var swfu_file02 = null;

swfupload_params.limit = 1;
swfupload_params.uploaded = 0;

function fileQueueError(file, errorCode, message) {
	if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
		$.dialog({message:'{lang admin.validate.swfupload.lang.limit}'});
	}else{
		$.dialog({message:'{lang admin.validate.swfupload.echo.fail}'});
	}
}

function uploadSuccess(file, serverData) {
	if(serverData.substring(0, 7) === "FILEID:") {
		var tempArr = serverData.split('|');
		var photoPath = formatFilePath(serverData.substring(7));
		
		$('#up_' + tempArr[5] + ' img').attr('src', photoPath).attr('data', serverData.substring(7));
	}else $.dialog({message:serverData});
}

function createSwfUpload(file, title){
	var swf_config = {
		upload_url: "{$ADMIN_SCRIPT}/poll/_upload&file=" + file + "&_SALT={$_var[current][SALT]}",
		
		file_size_limit : "200KB", 
		file_types : "*.jpg;*.bmp;*.gif;*.png",
		file_types_description : "JPG Images",
		file_upload_limit : "0",
		
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
		
		button_image_url : "static/images/swfupload/swfupload.png",
		button_placeholder_id : "swfu_" + file,
		button_width: 120,
		button_height: 18,
		button_text : '{lang admin.validate.swfupload.lang.button.choice}' + title + '({lang admin.validate.swfupload.lang.button.size.200})',
		button_text_style : '',
		button_text_top_padding: 0,
		button_text_left_padding: 18,
		
		flash_url : "static/swfupload.swf", 
		debug: false
	};
	
	return new SWFUpload(swf_config);
}

function check_form(frm){
	if(frm.txtIdentity.value.trim().length == 0){
		$(frm.txtIdentity).next('.help-inline').css('color', 'red');
    	frm.txtIdentity.focus();
    	return false;
    }
    
	if(frm.txtTitle.value.trim().length == 0){
		$(frm.txtTitle).next('.help-inline').css('color', 'red');
    	frm.txtTitle.focus();
    	return false;
    }
	
    frm.hdnFile01.value = $('#up_file01 img').attr('data') ? $('#up_file01 img').attr('data') : '';
    frm.hdnFile02.value = $('#up_file02 img').attr('data') ? $('#up_file02 img').attr('data') : '';
    
    $('#div_award .upload-button').each(function(index, item){
    	var ref = $(item).attr('ref');
    	var type = $(item).attr('type');
    	var data = $(item).attr('data');
    	
    	if(type == 'new') $(item).append('<input type="hidden" name="newfile[]" value="' + $('#up_' + ref + ' img').attr('data') + '"/>');
    	else $(item).append('<input type="hidden" name="file[' + data + ']" value="' + $('#up_' + ref + ' img').attr('data') + '"/>');
	});
	
    $('#div_poll_option .upload-button').each(function(index, item){
    	var ref = $(item).attr('ref');
    	var type = $(item).attr('type');
    	var data = $(item).attr('data');
    	
    	if(type == 'new') $(item).append('<input type="hidden" name="newoptionfile[]" value="' + $('#up_' + ref + ' img').attr('data') + '"/>');
    	else $(item).append('<input type="hidden" name="optionfile[' + data + ']" value="' + $('#up_' + ref + ' img').attr('data') + '"/>');
	});
	
    return true;
}

$(function(){
	$('.fancy').uniform();
	
	$('fieldset input,textarea,select').blur(function(){
		$(this).next('.help-inline').css('color', '#333');
	});
	
	$('.input-time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose:true, todayBtn:true});
	$('#txtExpried').datepicker({format:'yyyy-mm-dd'});
	
	$("input[name='rdoOptionType']").click(function(){
		if($(this).val() == '1') $("input[name='txtLimitNum']").val('1');
		else $("input[name='txtLimitNum']").val('');
	});
	
	$("input[name='rdoIsAward']").click(function(){
		if($(this).val() == '1') $('#div_award').show();
		else $('#div_award').hide();
	});
	
	$('.swfupload img').dblclick(function(){
		$(this).attr({src:'static/images/no-picture.jpg', data:''});
	});
	
	swfu_file01 = createSwfUpload('file01', '{lang poll.index_edit.view.file.01}');
	swfu_file02 = createSwfUpload('file02', '{lang poll.index_edit.view.file.02}');
	
	$('#btn_add_award').click(function(){
		award_count++;
		
    	var tbody = $('#tbl_body').get(0);
    	
    	var tempInsertRow = tbody.rows[tbody.rows.length - 2];
    	
    	var tempNewRow = tempInsertRow.cloneNode(true);
    	tempNewRow.style.display = '';
    	$(tempNewRow).attr('id', 'up_' + award_count);
    	
    	$('.upload-button', tempNewRow).attr('ref', award_count).html('<span id="swfu_' + award_count + '"></span>');
    	
    	$('img', tempNewRow).unbind('dblclick').dblclick(function(){
    		$(this).attr({src:'static/images/no-picture.jpg', data:''});
    	}).attr({src:'static/images/no-picture.jpg', data:''});
    	
    	var inputs = tempNewRow.getElementsByTagName('input');
    	for(var i = 0; i < inputs.length; i++){
    		if(inputs[i].name.indexOf('newdisplayorder') != -1 
    		|| inputs[i].name.indexOf('newcname') != -1 
    		|| inputs[i].name.indexOf('newnum') != -1) inputs[i].value = '';
    	}
    	
    	tbody.insertBefore(tempNewRow, tbody.rows[tbody.rows.length - 1]);
    	
    	createSwfUpload(award_count, '{lang poll.index_edit.view.file.item}');
    });
    
    $('#div_award .btn-primary').click(function(){
		$(this).parent().parent().remove();
	});
	
    $('#div_award .upload-button').each(function(index, item){
		var ref = $(item).attr('ref');
		$('span', item).attr('id', 'swfu_' + ref);
		
		createSwfUpload(ref, '{lang poll.index_edit.view.file.item}');
	});
	
	$('#btn_add_poll_option').click(function(){
    	poll_option_count++;
    	
		var tbody = $('#tbl_poll_option_body').get(0);
    	
    	var tempInsertRow = tbody.rows[tbody.rows.length - 2];
    	
    	var tempNewRow = tempInsertRow.cloneNode(true);
    	tempNewRow.style.display = '';
    	$(tempNewRow).attr('id', 'up_poll_option_' + poll_option_count);
    	
    	$('.upload-button', tempNewRow).attr('ref', 'poll_option_' + poll_option_count).html('<span id="swfu_poll_option_' + poll_option_count + '"></span>');
    	
    	$('img', tempNewRow).unbind('dblclick').dblclick(function(){
    		$(this).attr({src:'static/images/no-picture.jpg', data:''});
    	}).attr({src:'static/images/no-picture.jpg', data:''});
    	
    	var inputs = tempNewRow.getElementsByTagName('input');
    	for(var i = 0; i < inputs.length; i++){
    		if(inputs[i].name.indexOf('newoptiondisplayorder') != -1 
    		|| inputs[i].name.indexOf('newoptiontitle') != -1 
    		|| inputs[i].name.indexOf('newoptionsummary') != -1 ) inputs[i].value = '';
    	}
    	
    	tbody.insertBefore(tempNewRow, tbody.rows[tbody.rows.length - 1]);
    	
    	createSwfUpload('poll_option_' + poll_option_count, '{lang poll.index_edit.view.file.item}');
    });
    
    $('#div_poll_option .btn-primary').click(function(){
		$(this).parent().parent().remove();
	});
	
    $('#div_poll_option .upload-button').each(function(index, item){
		var ref = $(item).attr('ref');
		$('span', item).attr('id', 'swfu_' + ref);
		
		createSwfUpload(ref, '{lang poll.index_edit.view.file.item}');
	});
});
</script>
<!--{template /module/admin/view/foot}-->